package com.py.custom.tms.application.jvm.classload;

/**
 * 执行流程：加载、连接(验证、准备、解析)、初始化
 * 1、准备阶段
 *    给count1申请空间给定默认值(0)、
 *    给singleton申请空间给定默认值(null)、
 *    给count2申请空间给定默认值(0)
 * 2、初始化阶段（静态属性以及静态代码块从上到下执行）
 *    count1;
 *    singleton = new Singleton();做了count1++; count2++;
 *      所以：count1 = 1；count2 = 1；然后继续执行
 *    count2 = 0;
 *
 * 最终：
 *    count1: 1
 *    count2: 0
 */
public class MyTest6 {

    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        // 1
        System.out.println("count1: " + Singleton.count1);
        // 0
        System.out.println("count2: " + Singleton.count2);
    }
}

class Singleton {

    public static int count1;

    private static Singleton singleton = new Singleton();

    public static int count2 = 0;

    private Singleton() {
        count1++;
        count2++;
    }

    public static Singleton getInstance() {
        return singleton;
    }
}